﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Management;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using HYBatchPrinter.Utils;
using Microsoft.Win32;

namespace HYBatchPrinter
{
	public class PrinterFilter
	{
		public static IList<string> getPdfAndPltPrinterNames(PrintManager rvtPrinter)
		{
			string text = rvtPrinter.PrinterName;
			int num = 0;
			IList<string> list = new List<string>();
			List<string> allPrinterName = PrinterFilter.getAllPrinterName();
			List<string> invalidPrintDriver = PrinterFilter.GetInvalidPrintDriver();
			foreach (string text2 in allPrinterName)
			{
				if (PrinterFilter.IsReadyPrinting(text2, invalidPrintDriver))
				{
					rvtPrinter.SelectNewPrintDriver(text2);
					if (rvtPrinter.IsVirtual != 2 && rvtPrinter.IsVirtual != 3)
					{
						if (num == 0 && rvtPrinter.IsVirtual == 1)
						{
							text = text2;
							num++;
						}
						list.Add(text2);
					}
				}
			}
			rvtPrinter.SelectNewPrintDriver(text);
			return list;
		}

		private static Dictionary<string, string> getDicPrinterPortToCaption()
		{
			ManagementObjectCollection managementObjectCollection = new ManagementObjectSearcher("SELECT * FROM Win32_Printer").Get();
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (ManagementBaseObject managementBaseObject in managementObjectCollection)
			{
				ManagementBaseObject managementBaseObject2 = (ManagementObject)managementBaseObject;
				string text = null;
				string text2 = null;
				PropertyDataCollection.PropertyDataEnumerator enumerator2 = managementBaseObject2.Properties.GetEnumerator();
				while (enumerator2.MoveNext())
				{
					if (enumerator2.Current.Name == "PortName")
					{
						text = enumerator2.Current.Value.ToString();
					}
					else if (enumerator2.Current.Name == "Caption")
					{
						text2 = enumerator2.Current.Value.ToString();
					}
					if (text != null && text2 != null)
					{
						break;
					}
				}
				if (text != null && text2 != null && !dictionary.ContainsKey(text))
				{
					dictionary.Add(text, text2);
				}
			}
			return dictionary;
		}

		private static IList<string> getVirtualPrinterName(Dictionary<string, string> portToName)
		{
			IList<string> list = new List<string>();
			foreach (KeyValuePair<string, string> keyValuePair in portToName)
			{
				if (!PrinterFilter.isPhysicalPrinter(keyValuePair.Key))
				{
					list.Add(keyValuePair.Value);
				}
			}
			return list;
		}

		private static bool isPhysicalPrinter(string sPort)
		{
			bool result = false;
			if (sPort.Contains("LTP"))
			{
				result = true;
			}
			else if (sPort.Contains("COM"))
			{
				result = true;
			}
			else if (sPort.Contains("USB"))
			{
				result = true;
			}
			else if (sPort.Contains("192."))
			{
				result = true;
			}
			else if (sPort.Contains("\\\\"))
			{
				result = true;
			}
			else if (sPort.Contains("nul:"))
			{
				result = true;
			}
			return result;
		}

		public static void openWebWithdefaultScanning(string sWeb)
		{
			if (string.IsNullOrEmpty(sWeb))
			{
				return;
			}
			try
			{
				string input = Registry.ClassesRoot.OpenSubKey("http\\shell\\open\\command\\").GetValue("").ToString();
				MatchCollection matchCollection = new Regex("\"([^\"]+)\"").Matches(input);
				if (matchCollection.Count > 0)
				{
					Process.Start(matchCollection[0].Groups.get_Item(1).Value, sWeb);
				}
			}
			catch (Exception)
			{
			}
		}

		private static List<string> getAllPrinterName()
		{
			PrinterSettings.StringCollection installedPrinters = PrinterSettings.InstalledPrinters;
			string[] array = new string[installedPrinters.Count];
			installedPrinters.CopyTo(array, 0);
			List<string> list = new List<string>();
			foreach (string text in array)
			{
				if (!string.IsNullOrEmpty(text))
				{
					list.Add(text);
				}
			}
			if (list.Count != 0)
			{
				return list;
			}
			return null;
		}

		private static bool IsReadyPrinting(string sPrinterName, List<string> arInvalidDriver)
		{
			bool result = false;
			string printerDriverName = CSPrinterState.GetPrinterDriverName(sPrinterName);
			if (CSPrinterState.GetPrinterStatusInt(sPrinterName) == 0 && !arInvalidDriver.Contains(printerDriverName))
			{
				result = true;
			}
			return result;
		}

		private static List<string> GetInvalidPrintDriver()
		{
			List<string> list = new List<string>();
			string path = AppPathUtils.GetAppRootPath() + "\\Data\\InvalidPrintDriver.TXT";
			if (File.Exists(path))
			{
				FileStream fileStream = new FileStream(path, FileMode.Open);
				StreamReader streamReader = new StreamReader(fileStream);
				streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				while (streamReader.Peek() > 0)
				{
					string text = streamReader.ReadLine();
					if (!string.IsNullOrEmpty(text))
					{
						list.Add(text);
					}
				}
				streamReader.Close();
				fileStream.Close();
			}
			return list;
		}
	}
}
